using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Resources;
using System.Reflection;
using System.Threading;

namespace Microsoft.Samples.BizTalk.Adapter.Tcp
{
    /// <summary>
    ///     This class provides a helper function used to provide formatting for trace messages.
    /// </summary>
    /// <author>
    ///     Paul Brown
    /// </author>
    public class TraceHelper
    {
        #region Public Static Properties

        /// <summary>
        ///     This property provides the resource manager for the class library.
        /// </summary>
        public static readonly ResourceManager ResourceManager = new ResourceManager(ResourceConstants.BaseName, Assembly.GetExecutingAssembly());

        #endregion

        #region Public Static Methods

        /// <summary>
        ///     This method formats an entry after it has retrieved the format string from the resource assembly.
        /// </summary>
        public static string FormatEntry(string resourceName, params object[] arguments)
        {
            // Get resource
            string message = TraceHelper.ResourceManager.GetString(resourceName);

            // Format it
            if (arguments.Length > 0)
            {
                message = string.Format(message, arguments);
            }

            // Return formatted message
            return (TraceHelper.FormatRawEntry(message));
        }

        /// <summary>
        ///     This method formats a message.
        /// </summary>
        public static string FormatRawEntry(string message)
        {
            // Prefix date and time
            DateTime now = DateTime.Now;
            return (string.Format("{0} {1}.{2:000}:{3,3}: {4}", now.ToShortDateString(), now.ToLongTimeString(), now.Millisecond, Thread.CurrentThread.ManagedThreadId, message));
        }

        /// <summary>
        ///     This method writes a trace entry after it has retrieved the format string from the resource assembly.
        /// </summary>
        public static void WriteEntry(TraceLevel level, string resourceName, params object[] arguments)
        {
            // Get formatted message
            string message = TraceHelper.FormatEntry(resourceName, arguments);
            TraceHelper.WriteRawEntry(level, message);
        }

        /// <summary>
        ///     This method writes a pre-formatted trace entry.
        /// </summary>
        public static void WriteRawEntry(TraceLevel level, string message)
        {
            // Just write out pre-formatted message
            switch (level)
            {
                case TraceLevel.Error:

                    Trace.TraceError(message);
                    break;

                case TraceLevel.Warning:

                    Trace.TraceWarning(message);
                    break;

                case TraceLevel.Info:

                    Trace.TraceInformation(message);
                    break;

                case TraceLevel.Verbose:

                    Trace.WriteLine(message);
                    break;
            }
        }

        #endregion
    }
}
